

candidate_data <- read_csv("./data/candidate_data_raw.csv")

#recode main vars
estimation_data <-
  candidate_data %>%
  mutate(elected = (V_30A == "Ya, terpilih/lolos")*1) %>%
  group_by(ID) %>%
  mutate(elected = mean(elected, na.rm = T)) %>%
  #filter(!is.nan(elected)) %>%
  mutate(A = case_when(A == "Wave 1" ~ 1,
                       A == "Wave 2" ~ 2,
                       A == "Wave 3" ~ 3)) %>%
  mutate(elected = case_when(elected == 1 ~ "Elected",
                             elected == 0 ~ "Not Elected",
                             TRUE ~ NA_character_)) %>% #self reported win/lose, only asked in final wave. Note this isn't the win/lose variable used in the analyses, which is instead externally validated
  ungroup() %>%
  mutate(pre_post = case_when(A == "1" ~ 0,
                              A == "2" ~ 0,
                              A == "3" ~ 1)) %>% #these are waves


  
  #democracy outcomes -- positive pointed outcomes
  mutate_at(
    vars(V_29, V_32_A),
    funs(case_when(. == "Sangat setuju" ~ 4,
                   . == "Setuju" ~ 3,
                   . %in% c("Tidak setuju")  ~ 2,
                   . == "Sangat tidak setuju" ~ 1,
                   TRUE ~ NA_real_))) %>%
  #democracy outcomes -- negatively pointed outcomes
  mutate_at(
    vars(V_28, V_32_B, V_32_C),
    funs(case_when(. == "Sangat setuju" ~ 1,
                   . == "Setuju" ~ 2,
                   . %in% c("Tidak setuju")  ~ 3,
                   . == "Sangat tidak setuju" ~ 4,
                   TRUE ~ NA_real_))) %>%
  
  #trust in democracy institutions
  mutate_at(
    vars(V_31_A, V_31_B),
    funs(case_when(. == "Sangat percaya" ~ 4,
                   . == "Percaya" ~ 3,
                   . %in% c("Tidak percaya")  ~ 2,
                   . == "Sangat tidak percaya" ~ 1,
                   TRUE ~ NA_real_))) %>%
  
  
  #importance of internal factors
  mutate_at(
    vars(V_37_A, V_37_B, V_37_C, V_37_D, V_37_E, V_37_F),
    funs(case_when(. == "Sangat penting" ~ 4,
                   . == "Penting" ~ 3,
                   . %in% c("Tidak penting")  ~ 2,
                   . == "Sangat tidak penting" ~ 1,
                   TRUE ~ NA_real_))) %>%
  #external factors
  mutate_at(
    vars(V_38_A, V_38_B, V_38_C, V_38_D),
    funs(case_when(. == "Sangat penting" ~ 4,
                   . == "Penting" ~ 3,
                   . %in% c("Tidak penting")  ~ 2,
                   . == "Sangat tidak penting" ~ 1,
                   TRUE ~ NA_real_))) %>%
  
  #outgroup animosity outcomes
  mutate(V_17_A = case_when(V_6 != "Cina/Tionghoa" & V_17_A == "Sangat suka" ~ 1,
                            V_6 != "Cina/Tionghoa" & V_17_A == "Suka" ~ 2,
                            V_6 != "Cina/Tionghoa" & V_17_A == "Netral" ~ 3,
                            V_6 != "Cina/Tionghoa" & V_17_A == "Tidak suka"  ~ 4,
                            V_6 != "Cina/Tionghoa" & V_17_A == "Sangat tidak suka" ~ 5,
                            TRUE ~ NA_real_),
         V_17_C = case_when(V_5 %in% c("Islam", "Hindu", "Buddha") & V_17_C == "Sangat suka" ~ 1,
                            V_5 %in% c("Islam", "Hindu", "Buddha") & V_17_C == "Suka" ~ 2,
                            V_5 %in% c("Islam", "Hindu", "Buddha") & V_17_C == "Netral" ~ 3,
                            V_5 %in% c("Islam", "Hindu", "Buddha") & V_17_C == "Tidak suka"  ~ 4,
                            V_5 %in% c("Islam", "Hindu", "Buddha") & V_17_C == "Sangat tidak suka" ~ 5,
                            TRUE ~ NA_real_),
         V_17_D = case_when(V_5 != "Islam" & V_17_D == "Sangat suka" ~ 1,
                            V_5 != "Islam" & V_17_D == "Suka" ~ 2,
                            V_5 != "Islam" & V_17_D == "Netral" ~ 3,
                            V_5 != "Islam" & V_17_D == "Tidak suka"  ~ 4,
                            V_5 != "Islam" & V_17_D == "Sangat tidak suka" ~ 5,
                            TRUE ~ NA_real_),
         #partisan animosity outcomes, depends on presidential coalitional membership
         V_17_F = case_when(D %in% c("PDIP", "PKS", "NASDEM", "PKB", "PERINDO", "PPP") & V_17_F == "Sangat suka" ~ 1,
                            D %in% c("PDIP", "PKS", "NASDEM", "PKB", "PERINDO", "PPP") & V_17_F == "Suka" ~ 2,
                            D %in% c("PDIP", "PKS", "NASDEM", "PKB", "PERINDO", "PPP") & V_17_F == "Netral" ~ 3,
                            D %in% c("PDIP", "PKS", "NASDEM", "PKB", "PERINDO", "PPP") & V_17_F == "Tidak suka"  ~ 4,
                            D %in% c("PDIP", "PKS", "NASDEM", "PKB", "PERINDO", "PPP") & V_17_F == "Sangat tidak suka" ~ 5,
                            TRUE ~ NA_real_),
         V_17_G = case_when(D %in% c("PDIP", "GERINDRA", "GOLKAR", "PAN", "DEMOKRAT", "PERINDO", "PPP") & V_17_G == "Sangat suka" ~ 1,
                            D %in% c("PDIP", "GERINDRA", "GOLKAR", "PAN", "DEMOKRAT", "PERINDO", "PPP") & V_17_G == "Suka" ~ 2,
                            D %in% c("PDIP", "GERINDRA", "GOLKAR", "PAN", "DEMOKRAT", "PERINDO", "PPP") & V_17_G == "Netral" ~ 3,
                            D %in% c("PDIP", "GERINDRA", "GOLKAR", "PAN", "DEMOKRAT", "PERINDO", "PPP") & V_17_G == "Tidak suka"  ~ 4,
                            D %in% c("PDIP", "GERINDRA", "GOLKAR", "PAN", "DEMOKRAT", "PERINDO", "PPP") & V_17_G == "Sangat tidak suka" ~ 5,
                            TRUE ~ NA_real_),
         V_17_H = case_when(D %in% c("GERINDRA", "GOLKAR", "PAN", "DEMOKRAT", "PKS", "NASDEM", "PKB") & V_17_H == "Sangat suka" ~ 1,
                            D %in% c("GERINDRA", "GOLKAR", "PAN", "DEMOKRAT", "PKS", "NASDEM", "PKB") & V_17_H == "Suka" ~ 2,
                            D %in% c("GERINDRA", "GOLKAR", "PAN", "DEMOKRAT", "PKS", "NASDEM", "PKB") & V_17_H == "Netral" ~ 3,
                            D %in% c("GERINDRA", "GOLKAR", "PAN", "DEMOKRAT", "PKS", "NASDEM", "PKB") & V_17_H == "Tidak suka"  ~ 4,
                            D %in% c("GERINDRA", "GOLKAR", "PAN", "DEMOKRAT", "PKS", "NASDEM", "PKB") & V_17_H == "Sangat tidak suka" ~ 5,
                            TRUE ~ NA_real_)) %>%
         
  
  mutate_at(vars(V_20_B, V_20_D, V_20_G, V_20_F, V_20_H), funs(case_when(. == "Sangat setuju" ~ 4,
                                                                         . == "Setuju" ~ 3,
                                                                         . == "Tidak setuju" ~ 2,
                                                                         . == "Sangat tidak setuju" ~ 1,
                                                                         TRUE ~ NA_real_))) %>%
  mutate_at(vars(V_20_A, V_20_C, V_20_E), funs(case_when(. == "Sangat setuju" ~ 1,
                                                         . == "Setuju" ~ 2,
                                                         . == "Tidak setuju" ~ 3,
                                                         . == "Sangat tidak setuju" ~ 4,
                                                         TRUE ~ NA_real_))) %>%
  
  mutate_at(vars(V_21_A, V_21_B, V_21_C, V_21_D, V_21_E, V_21_F), funs(case_when(. == "Sangat penting" ~ 4,
                                                                                 . == "Penting" ~ 3,
                                                                                 . == "Tidak penting" ~ 2,
                                                                                 . == "Sangat tidak penting" ~ 1,
                                                                                 TRUE ~ NA_real_))) %>%
  
  mutate_at(vars(V_22_A, V_22_B, V_22_D, V_22_E), funs(case_when(. == "Sangat setuju" ~ 4,
                                                                 . == "Setuju" ~ 3,
                                                                 . == "Tidak setuju" ~ 2,
                                                                 . == "Sangat tidak setuju" ~ 1,
                                                                 TRUE ~ NA_real_))) %>%
  mutate(V_22_C = case_when(V_22_C == "Sangat setuju" ~ 1,
                            V_22_C == "Setuju" ~ 2,
                            V_22_C == "Tidak setuju" ~ 3,
                            V_22_C == "Sangat tidak setuju" ~ 4,
                            TRUE~ NA_real_)) %>%
  ungroup() %>%
  rowwise() %>%
  mutate(
         avg_score_trust_inst = mean(c(V_27_A, V_27_D, V_27_E), na.rm = T),
         avg_score_trust_ppl = mean(c(V_26_A, V_26_B, V_26_C, V_26_D), na.rm = T),
         avg_score_democ_support = mean(c(V_32_A, V_32_B, V_32_C), na.rm = T),
         avg_score_trust_elect = mean(c(V_28, V_29, V_31_A, V_31_B), na.rm = T),
         avg_score_internal = mean(c(V_37_A, V_37_B, V_37_C, V_37_D, V_37_E, V_37_F), na.rm = T),
         avg_score_external = mean(c(V_38_A, V_38_B, V_38_C, V_38_D), na.rm = T),
         avg_score_dislike_group = mean(c(V_17_A, V_17_C, V_17_D), na.rm = T),
         avg_score_dislike_pol = mean(c(V_17_F, V_17_G, V_17_H), na.rm = T),
         avg_score_rwa_allwaves = mean(c(V_22_A,V_22_B, V_22_C, V_22_D, V_22_E), na.rm = T),
         avg_score_neurot_allwaves = mean(c(V_20_A, V_20_B, V_20_C, V_20_D), na.rm = T),
         avg_score_openness_allwaves = mean(c(V_20_E, V_20_F, V_20_G, V_20_H), na.rm = T))

#heterogeneity variables
traits_wave1 <-
  estimation_data %>%
  filter(A == 1) %>%
  rowwise() %>%
  mutate(avg_neurot = mean(c(V_20_A, V_20_B, V_20_C, V_20_D), na.rm = T)) %>%
  mutate(avg_open = mean(c(V_20_E, V_20_F, V_20_G, V_20_H), na.rm = T)) %>%
  mutate(avg_rwa = mean(c(V_22_A,V_22_B, V_22_C, V_22_D, V_22_E), na.rm = T)) %>%
  mutate(internal_reasons = mean(c(V_21_A,V_21_B), na.rm = T),
         external_reasons = mean(c(V_21_C, V_21_D, V_21_E, V_21_F))) %>%
  dplyr::select(ID, avg_neurot, avg_open, avg_rwa, internal_reasons, external_reasons)


campaign_data <-
  estimation_data %>%
  filter(A == 1) %>%
  select(ID, V_44, V_14) %>%
  mutate(campaign_expend = case_when(V_44 %in% c("< 10 juta", "10 - 50 juta") ~ "Low expenditure",
                                     V_44 %in% c("50 - 100 juta", "100 - 500 juta", "500 juta - 1 miliar", "> 1 miliar") ~ "High expenditure",
                                     TRUE ~ NA_character_)) %>%
  mutate(campaign_prob_success = case_when(V_14 == "Sangat besar kemungkinannya (80-100%)" ~ "High chance",
                                           TRUE ~ "Low chance")) %>%
  select(-c(V_14, V_44))

estimation_data %<>%
  left_join(traits_wave1) %>%
  left_join(campaign_data)

estimation_data$E
#make covariates
estimation_data <-
  estimation_data %>%
  mutate(javanese_recode_plot = case_when(V_6 == "Jawa" ~ "Javanese",
                                          TRUE ~ "Other ethnicity"),
         education_recode_plot = case_when(V_3 == "Sekolah Menengah Atas (SMA) atau sederajat" ~ "High school",
                                           V_3 %in% c("Diploma (D1/D2/D3)", "S1") ~ "Bachelor's",
                                           V_3 %in% c("S2", "S3") ~ "Postgraduate",
                                           TRUE ~ NA_character_),
         islam_recode_plot = case_when(V_5 == "Islam" ~ "Islam",
                                       TRUE ~ "Other religion"),
         gender_recode_plot = case_when(V_1 == "Laki-laki" ~ "Man",
                                        V_1 == "Perempuan" ~ "Woman"),
         islam_party_recode_plot = case_when(D %in% c("PKS", "PPP", "PKB") ~ "Islamist",
                                             TRUE ~ "Not Islamist"),
         pdip_recode_plot = case_when(D %in% c("PDIP") ~ "PDIP",
                                      TRUE ~ "Not PDIP"),
         age_recode_plot = case_when(V_2 < 30 ~ "< 30",
                                     V_2 > 30 & V_2 < 40 ~ "30-40",
                                     V_2 > 40 & V_2 < 50 ~ "40-50",
                                     V_2 > 50 ~ "> 50"),
         list_position_recode_plot = case_when(E == "Nomor Urut 1" ~ 1,
                                               E == "Nomor Urut 2" ~ 2,
                                               E == "Nomor Urut 3" ~ 3,
                                               E %in% c("4", "Nomor Urut 4") ~ 4),
         income_recode_plot = case_when(
           V_60 %in% c("Kurang dari Rp 5.000.000", "Rp 5.000.000 - Rp 5.999.999", "Rp 6.000.000 - Rp 6.999.999", "Rp 7.000.000 - Rp 7.999.999",
                       "Rp 8.000.000 - Rp 8.999.999", "Rp 9.000.000 - Rp 9.999.999") ~ "<10m IDR",
           V_60 %in% c("Rp 10.000.000 - Rp 10.999.999", "Rp 11.000.000 - Rp 11.999.999", "Rp 12.000.000 - Rp 12.999.999",
                       "Rp 13.000.000 - Rp 13.999.999", "Rp 14.000.000 - Rp 14.999.999",
                       "Rp 15.000.000 - Rp 15.999.999", "Rp 16.000.000 - Rp 16.999.999", "Rp 17.000.000 - Rp 17.999.999",
                       "Rp 18.000.000 - Rp 18.999.999", "Rp 19.000.000 - Rp 19.999.999") ~ "10-20m IDR",
           V_60 %in% c("Rp 20.000.000 - Rp 24.999.999", "Rp 25.000.000 - Rp 29.999.999", "Rp 25.000.000 - Rp 29.999.999_duplicated_19", "Rp 30.000.000 - Rp 34.999.999",
                       "Rp 35.000.000 - Rp 39.999.999", "Rp 40.000.000 - Rp 44.999.999", "Rp 45.000.000 - Rp 49.999.999", "Rp 50.000.000 - Rp 59.999.999",
                       "Rp 60.000.000 - Rp 69.999.999", "Rp 70.000.000 - Rp 79.999.999", "Rp 80.000.000 - Rp 89.999.999", "Rp 90.000.000 - Rp 99.999.999",
                       "Rp 100.000.000 - Rp 200.000.000", "Lebih dari Rp 200.000.000") ~ ">20m IDR",
           TRUE ~ NA_character_),
         income_recode_plot_2 = case_when(
           V_60 %in% c("Kurang dari Rp 5.000.000") ~ "<5m IDR",
           V_60 %in% c("Rp 5.000.000 - Rp 5.999.999", "Rp 6.000.000 - Rp 6.999.999", "Rp 7.000.000 - Rp 7.999.999",
                       "Rp 8.000.000 - Rp 8.999.999", "Rp 9.000.000 - Rp 9.999.999") ~ "5-10m IDR",
           V_60 %in% c("Rp 10.000.000 - Rp 10.999.999", "Rp 11.000.000 - Rp 11.999.999", "Rp 12.000.000 - Rp 12.999.999",
                       "Rp 13.000.000 - Rp 13.999.999", "Rp 14.000.000 - Rp 14.999.999") ~ "10-15m IDR",
           V_60 %in% c("Rp 15.000.000 - Rp 15.999.999", "Rp 16.000.000 - Rp 16.999.999", "Rp 17.000.000 - Rp 17.999.999",
                       "Rp 18.000.000 - Rp 18.999.999", "Rp 19.000.000 - Rp 19.999.999") ~ "15-20m IDR",
           V_60 %in% c("Rp 20.000.000 - Rp 24.999.999") ~ "20-25m IDR",
           V_60 %in% c("Rp 25.000.000 - Rp 29.999.999", "Rp 25.000.000 - Rp 29.999.999_duplicated_19") ~ "25-30m IDR",
           V_60 %in% c("Rp 30.000.000 - Rp 34.999.999",
                       "Rp 35.000.000 - Rp 39.999.999", "Rp 40.000.000 - Rp 44.999.999", "Rp 45.000.000 - Rp 49.999.999", "Rp 50.000.000 - Rp 59.999.999",
                       "Rp 60.000.000 - Rp 69.999.999", "Rp 70.000.000 - Rp 79.999.999", "Rp 80.000.000 - Rp 89.999.999", "Rp 90.000.000 - Rp 99.999.999",
                       "Rp 100.000.000 - Rp 200.000.000", "Lebih dari Rp 200.000.000") ~ ">30m IDR",
           TRUE ~ NA_character_),
         #these are median values
         internal_reasons_recode_plot = case_when(internal_reasons > 2.42 ~ "High motivation",
                                                  TRUE ~ "Low motivation"),
         rwa_recode_plot = case_when(avg_rwa > 3.24 ~ "High RWA",
                                     TRUE ~ "Low RWA"),
         neurot_recode_plot = case_when(avg_neurot > 2.04 ~ "High neuroticism",
                                        TRUE ~ "Low neuroticism"),
         open_recode_plot = case_when(avg_open > 2.66 ~ "High openness",
                                      TRUE ~ "Low openness")) %>%
  mutate(education_recode_plot = factor(education_recode_plot, levels = c("High school", "Bachelor's", "Postgraduate")),
         gender_recode_plot = factor(gender_recode_plot, levels = c("Man", "Woman")),
         islam_recode_plot = factor(islam_recode_plot, levels = c("Islam", "Other religion")),
         income_recode_plot = factor(income_recode_plot, levels = c("<10m IDR", "10-20m IDR", ">20m IDR")),
         javanese_recode_plot = factor(javanese_recode_plot, levels = c("Javanese", "Other ethnicity")),
         age_recode_plot = factor(age_recode_plot, levels = c("< 30", "30-40", "40-50", "> 50")),
         islam_party_recode_plot = factor(islam_party_recode_plot, levels = c("Islamist", "Not Islamist")),
         pdip_recode_plot = factor(pdip_recode_plot, levels = c("PDIP", "Not PDIP")))


#make incumbency

estimation_data <-
  estimation_data %>%
  mutate(incumbent_flag = case_when(str_detect(V_4, "ANGGOTA|DPRD") ~ "Incumbent",
                                    TRUE ~ "Not Incumbent"))




